﻿// NahidaProject-System-Service-BuildExample.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

class Service : public WindowsService {
	using WindowsService::WindowsService;

protected:
	virtual DWORD WINAPI worker(LPVOID) {
		//  Periodically check if the service has been requested to stop
		while (WaitForSingleObject(stopEvent, 0) != WAIT_OBJECT_0) {

			// Pause on pauseEvent
			if (WaitForSingleObject(pauseEvent, 0) != WAIT_OBJECT_0) {

				/*
				* Perform main service functionality here
				*/

				//  Simulate work
				Sleep(3000);

			}
			else {
				confirm_pause();
				// Wait for continue to be thrown
				WaitForSingleObject(continueEvent, INFINITE);
				confirm_continue();
			}
		}

		return ERROR_SUCCESS;
	}

	// Gets called during startup.
	virtual void on_startup() {
		/*
		 * Perform tasks necessary to start the service here.
		 */
	}

	// Gets called when pause is thrown and pause/continue is enabled.
	virtual void on_pause() {
		/*
		 * Perform tasks necessary to pause the service here.
		 */
	}

	// Gets called when continue is thrown and pause/continue is enabled.
	virtual void on_continue() {
		/*
		 * Perform tasks necessary to continue the service here.
		 */
	}

	// Gets called when the windows service controller tells service to stop, but BEFORE stopEvent is thrown.
	virtual void on_stop() {
		/*
		 * Perform tasks necessary to stop the service loop here.
		 */
	}

	// Gets called all the way at the end of the liecycle.
	virtual void on_exit() {
		/*
		 * Perform tasks necessary for cleanup.
		 */
	}

	// What to do when the service fails to regiser.
	// By default it runs test_startStop();
	virtual void on_failedRegistration() {
		/*
		 * what to do when registration fails...
		 */
		test_startStop();
	}
};

int main(int, TCHAR) {
	Service testService("TestService", true);
	return testService.run();
}

// 运行程序: Ctrl + F5 或调试 >“开始执行(不调试)”菜单
// 调试程序: F5 或调试 >“开始调试”菜单

// 入门使用技巧: 
//   1. 使用解决方案资源管理器窗口添加/管理文件
//   2. 使用团队资源管理器窗口连接到源代码管理
//   3. 使用输出窗口查看生成输出和其他消息
//   4. 使用错误列表窗口查看错误
//   5. 转到“项目”>“添加新项”以创建新的代码文件，或转到“项目”>“添加现有项”以将现有代码文件添加到项目
//   6. 将来，若要再次打开此项目，请转到“文件”>“打开”>“项目”并选择 .sln 文件
